package com.enjoyor.soa.traffic.core.efoms.service.impl;

import com.enjoyor.soa.traffic.core.efoms.domain.EvalGradeInfo;
import com.enjoyor.soa.traffic.core.efoms.domain.FileInfo;
import com.enjoyor.soa.traffic.core.efoms.domain.WorkordersInfo;
import com.enjoyor.soa.traffic.core.efoms.domain.WorkordersRecordInfo;
import com.enjoyor.soa.traffic.core.efoms.enums.EnumDicPublic;
import com.enjoyor.soa.traffic.core.efoms.enums.EnumMessage;
import com.enjoyor.soa.traffic.core.efoms.helper.GetObjectHelper;
import com.enjoyor.soa.traffic.core.efoms.mapper.IEvalGradeInfoMapper;
import com.enjoyor.soa.traffic.core.efoms.mapper.IFileInfoMapper;
import com.enjoyor.soa.traffic.core.efoms.mapper.IWorkordersInfoMapper;
import com.enjoyor.soa.traffic.core.efoms.mapper.IWorkordersRecordMapper;
import com.enjoyor.soa.traffic.core.efoms.service.IWorkordersInfoService;
import com.enjoyor.soa.traffic.frame.logger.IPubLogMapper;
import com.enjoyor.soa.traffic.util.enums.log.LogInfoDic;
import com.enjoyor.soa.traffic.util.helper.LoggerHelper;
import com.enjoyor.soa.traffic.util.helper.TimeHelper;
import com.enjoyor.soa.traffic.util.pojo.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional
@SuppressWarnings("rawtypes")
public class WorkordersInfoService implements IWorkordersInfoService {

    @Autowired
    private IWorkordersInfoMapper workordersInfoMapper;

    @Autowired
    private IWorkordersRecordMapper workordersRecordMapper;

    @Autowired
    private IFileInfoMapper fileInfoMapper;
    
    @Autowired
    private IEvalGradeInfoMapper evalGradeInfoMapper;
    
    @Autowired
    private IPubLogMapper pubLogMapper;

    @Override
    public WorkordersInfo selectWorkordersBaseById(String workordersId) {
    	try {
	    	WorkordersInfo workordersInfo = workordersInfoMapper.selectById(workordersId);
	        return workordersInfo;
	    } catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
    }
    
    @Override
    public WorkordersInfo selectWorkordersInfoById(String workordersId){
    	try {
	    	Map<String,Object> recordMap = new HashMap<String ,Object>();
			recordMap.put("workordersId",workordersId);
			List<WorkordersRecordInfo> workordersRecordList = workordersRecordMapper.selectWorkordersRecordList(recordMap);
			List<WorkordersRecordInfo> submitList = new ArrayList<WorkordersRecordInfo>();  //立案
			List<WorkordersRecordInfo> cancelList = new ArrayList<WorkordersRecordInfo>();   //撤销
			List<WorkordersRecordInfo> pressList = new ArrayList<WorkordersRecordInfo>();   //催办
			List<WorkordersRecordInfo> dispatchList = new ArrayList<WorkordersRecordInfo>();  //派发
			List<WorkordersRecordInfo> postponeList = new ArrayList<WorkordersRecordInfo>();   //延期申请
			List<WorkordersRecordInfo> auditList = new ArrayList<WorkordersRecordInfo>();   //延期审核
			List<WorkordersRecordInfo> fackbackList = new ArrayList<WorkordersRecordInfo>();  //反馈
			List<WorkordersRecordInfo> sureList = new ArrayList<WorkordersRecordInfo>();   //确认
			List<WorkordersRecordInfo> evaluateList = new ArrayList<WorkordersRecordInfo>();   //评价
	    	for(WorkordersRecordInfo workordersRecord : workordersRecordList) {
	    		String operTypeCode = workordersRecord.getOperTypeCode();
	    		if(EnumDicPublic.ORD_OPER_SUBMIT.getKey().equals(operTypeCode)) {
	    			submitList.add(workordersRecord);
	    		}else if(EnumDicPublic.ORD_OPER_DELETE.getKey().equals(operTypeCode)) {
	    			cancelList.add(workordersRecord);
	    		}else if(EnumDicPublic.ORD_OPER_PRESS.getKey().equals(operTypeCode)) {
	    			pressList.add(workordersRecord);
	    		}else if(EnumDicPublic.ORD_OPER_DISPATCH.getKey().equals(operTypeCode)) {
	    			dispatchList.add(workordersRecord);
	    		}else if(EnumDicPublic.ORD_OPER_POSTPONE.getKey().equals(operTypeCode)) {
	    			postponeList.add(workordersRecord);
	    		}else if(EnumDicPublic.ORD_OPER_CHECK.getKey().equals(operTypeCode)) {
	    			auditList.add(workordersRecord);
	    		}else if(EnumDicPublic.ORD_OPER_FACKBACK.getKey().equals(operTypeCode)) {
	    			fackbackList.add(workordersRecord);
	    		}else if(EnumDicPublic.ORD_OPER_SURE.getKey().equals(operTypeCode)
	    				|| EnumDicPublic.ORD_OPER_AGAINSURE.getKey().equals(operTypeCode)) {
	    			sureList.add(workordersRecord);
	    		}else if(EnumDicPublic.ORD_OPER_EVALUATE.getKey().equals(operTypeCode)) {
	    			List<EvalGradeInfo> evalGradeList = evalGradeInfoMapper.selectEvalGradeList(recordMap);
	    			if(null != evalGradeList && evalGradeList.size() > 0 ) {
	    				workordersRecord.setEvalGradeList(evalGradeList);
	    			}
	    			evaluateList.add(workordersRecord);
	    		}
	    	}
	    	Map<String,List<WorkordersRecordInfo>> workordersRecordMap = new HashMap<String ,List<WorkordersRecordInfo>>();
	    	workordersRecordMap.put("submitList", submitList);
	    	workordersRecordMap.put("cancelList", cancelList);
	    	workordersRecordMap.put("pressList", pressList);
	    	workordersRecordMap.put("dispatchList", dispatchList);
	    	workordersRecordMap.put("postponeList", postponeList);
	    	workordersRecordMap.put("auditList", auditList);
	    	workordersRecordMap.put("fackbackList", fackbackList);
	    	workordersRecordMap.put("sureList", sureList);
	    	workordersRecordMap.put("evaluateList", evaluateList);
	    	WorkordersInfo workordersInfo = this.selectWorkordersBaseById(workordersId);
	    	if(null != workordersInfo) {
	    		workordersInfo.setWorkordersRecordMap(workordersRecordMap);
	    	}
	        return workordersInfo;
	    } catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
    }
    
    @Override
	public List<WorkordersInfo> selectWorkordersInfoList(String workordersId, String repStartDate, String repEndDate,
			String devCategoryCode, String devTypeCode, String devId, String devName, String key, String devAreaCode,
			String devDeptId, String failureTypeCode, String failureDescrible, String repSourceCode,
			String workordersStatusCode, String lableClick){
    	try {
	    	Map<String,Object> paramT = new HashMap<String ,Object>();
	    	paramT.put("workordersId", workordersId);
	    	paramT.put("repStartDate", repStartDate);
	    	paramT.put("repEndDate", repEndDate);
	    	paramT.put("devCategoryCode", devCategoryCode);
	    	paramT.put("devTypeCode", devTypeCode);
	    	paramT.put("devId", devId);
	    	paramT.put("devName", devName);
	    	paramT.put("key", key);
	    	paramT.put("devAreaCode", devAreaCode);
	    	paramT.put("devDeptId", devDeptId);
	    	paramT.put("failureTypeCode", failureTypeCode);
	    	paramT.put("failureDescrible", failureDescrible);
	    	paramT.put("repSourceCode", repSourceCode);
	    	paramT.put("workordersStatusCode", workordersStatusCode);
	    	paramT.put("lableClick", lableClick);
	    	return workordersInfoMapper.selectWorkordersInfoList(paramT);
	    } catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
    }
    @Override
    public List<WorkordersInfo> selectWorkordersInfoList(String devCategoryCode, String devTypeCode, String devId){
    	try {
	    	Map<String,Object> paramT = new HashMap<String ,Object>();
	    	paramT.put("devCategoryCode", devCategoryCode);
	    	paramT.put("devTypeCode", devTypeCode);
	    	paramT.put("devId", devId);
	    	List<String> workordersStatusArr = new ArrayList<String>();
	    	workordersStatusArr.add(EnumDicPublic.ORD_WAITREPAIR.getKey());
	    	workordersStatusArr.add(EnumDicPublic.ORD_WAITDISPATCH.getKey());
	    	workordersStatusArr.add(EnumDicPublic.ORD_WAITSURE.getKey());
	    	workordersStatusArr.add(EnumDicPublic.ORD_WAITAGAINSURE.getKey());
	    	workordersStatusArr.add(EnumDicPublic.ORD_ISREJECT.getKey());
	    	paramT.put("workordersStatusArr", workordersStatusArr);
	    	return workordersInfoMapper.selectWorkordersInfoList(paramT);
    	} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
    }
	
    @Override
    public Page selectWorkordersInfoPage(Page page, String workordersId, String repStartDate, String repEndDate,
			String devCategoryCode, String devTypeCode, String devId, String devName, String key, String devAreaCode,
			String devDeptId, String failureTypeCode, String failureDescrible, String repSourceCode,
			String workordersStatusCode, String orderSeq, String orderMode, String lableClick, List<String> noStatusCodeList){
    	try {
	    	Map<String,Object> mapBean = new HashMap<String,Object>();
	    	mapBean.put("workordersId", workordersId);
	    	mapBean.put("repStartDate", repStartDate);
	    	mapBean.put("repEndDate", repEndDate);
	    	mapBean.put("devCategoryCode", devCategoryCode);
	    	mapBean.put("devTypeCode", devTypeCode);
	    	mapBean.put("devId", devId);
	    	mapBean.put("devName", devName);
	    	mapBean.put("key", key);
	    	mapBean.put("devAreaCode", devAreaCode);
	    	mapBean.put("devDeptId", devDeptId);
	    	mapBean.put("failureTypeCode", failureTypeCode);
	    	mapBean.put("failureDescrible", failureDescrible);
	    	mapBean.put("repSourceCode", repSourceCode);
	    	mapBean.put("workordersStatusCode", workordersStatusCode);
	    	mapBean.put("orderSeq", orderSeq);
	    	mapBean.put("orderMode", orderMode);
	    	mapBean.put("lableClick", lableClick);
	    	if (null != noStatusCodeList  && noStatusCodeList.size() > 0) {
	    		mapBean.put("noStatusCodeList", noStatusCodeList);
			}
	    	page.setMapBean(mapBean);
	        return workordersInfoMapper.selectPageInfo(page);
    	} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
    }
    
    @Override
    public String insertWorkordersInfo(WorkordersInfo workordersInfo, String personName){
    	try {
	    	// 默认任务信息
	    	String workordersId = workordersInfo.getWorkordersId();
	    	if(null == workordersId && "".equals(workordersId)) {
	    		workordersId = "W" + TimeHelper.getUniqueString();
	    		workordersInfo.setWorkordersId(workordersId);
	    	}
	    	String repDate = workordersInfo.getRepDate();
	    	if(null == repDate || "".equals(repDate)) {
	    		workordersInfo.setRepDate(TimeHelper.dateToLongString());
	    	}
	        workordersInfo.setCreateDate(TimeHelper.dateToLongString());
	        workordersInfo.setPressTimes(0);
	        workordersInfo.setIsDefer(false);
	
	    	String workordersStatusCode = workordersInfo.getWorkordersStatusCode();
	        if(null == workordersStatusCode && "".equals(workordersStatusCode)) {
	        	workordersInfo.setWorkordersStatusCode(EnumDicPublic.ORD_WAITDISPATCH.getKey());
	            workordersInfo.setWorkordersStatusName(EnumDicPublic.ORD_WAITDISPATCH.getValue());
	    	}else {
	    		workordersInfo.setWorkordersStatusCode(workordersStatusCode);
	            workordersInfo.setWorkordersStatusName(GetObjectHelper.getNameByStatusCode(workordersStatusCode));
	    	}
	    	//保存附件信息
	    	List<FileInfo> fileInfoList = workordersInfo.getFileInfoList();
	    	if(null != fileInfoList && fileInfoList.size() > 0) {
	    		//fileInfoMapper.insertFileBatch(fileInfoList);
	    		for(FileInfo fileInfo : fileInfoList) {
	        		String fileId = "F" + TimeHelper.getUniqueString();
	        		fileInfo.setFileId(fileId);
	        		fileInfo.setReleteId(workordersId);
	        		fileInfoMapper.insert(fileInfo);
	        	}
	    	}
	    	//新增立案操作记录
	    	WorkordersRecordInfo submitRecord = new WorkordersRecordInfo();
	    	submitRecord.setWorkordersRecordId("WR" + TimeHelper.getUniqueString());
	    	submitRecord.setWorkordersId(workordersId);
	    	submitRecord.setOperTypeCode(EnumDicPublic.ORD_OPER_SUBMIT.getKey());
	    	submitRecord.setOperTypeName(EnumDicPublic.ORD_OPER_SUBMIT.getValue());
	    	submitRecord.setOperPerson(personName);
	    	submitRecord.setOperDate(TimeHelper.dateToLongString());
	    	submitRecord.setEndStatusCode(workordersInfo.getWorkordersStatusCode());
	    	workordersRecordMapper.insert(submitRecord);
	    	String deadlineTime = null;
	    	//保存派发、反馈操作记录
	    	Map<String,List<WorkordersRecordInfo>> workordersRecordMap = workordersInfo.getWorkordersRecordMap();
	    	if(null != workordersRecordMap) {
	    		List<WorkordersRecordInfo> dispatchList = workordersRecordMap.get("dispatchList");
	        	if(null != dispatchList && dispatchList.size() > 0) {
	        		for(WorkordersRecordInfo dispatchRecord : dispatchList) {
	        			//deadlineTime维修期限需赋值到维修工单
	        			deadlineTime = dispatchRecord.getDeadlineDate();
	        			dispatchRecord.setWorkordersRecordId("WR" + TimeHelper.getUniqueString());
	        			dispatchRecord.setWorkordersId(workordersId);
	        			dispatchRecord.setOperTypeCode(EnumDicPublic.ORD_OPER_DISPATCH.getKey());
	        			dispatchRecord.setOperTypeName(EnumDicPublic.ORD_OPER_DISPATCH.getValue());
	        			dispatchRecord.setOperPerson(personName);
	        			dispatchRecord.setOperDate(TimeHelper.dateToLongString());
	        	    	workordersRecordMapper.insert(dispatchRecord);
	            	}
	        	}
	        	List<WorkordersRecordInfo> fackbackList = workordersRecordMap.get("fackbackList");
	        	if(null != fackbackList && fackbackList.size() > 0) {
	        		for(WorkordersRecordInfo fackbackRecord : fackbackList) {
	        			String workordersRecordId = "WR" + TimeHelper.getUniqueString();
	        			fackbackRecord.setWorkordersRecordId(workordersRecordId);
	        			fackbackRecord.setWorkordersId(workordersId);
	        			fackbackRecord.setOperTypeCode(EnumDicPublic.ORD_OPER_FACKBACK.getKey());
	        			fackbackRecord.setOperTypeName(EnumDicPublic.ORD_OPER_FACKBACK.getValue());
	        			fackbackRecord.setOperResultCode(EnumDicPublic.OPER_FACKBACK_OVER.getKey());
	        			fackbackRecord.setOperResult(EnumDicPublic.OPER_FACKBACK_OVER.getValue());
	        			fackbackRecord.setOperPerson(personName);
	        			fackbackRecord.setOperDate(TimeHelper.dateToLongString());
	        	    	workordersRecordMapper.insert(fackbackRecord);
	        	    	
	        	    	List<FileInfo> fileRecordList = fackbackRecord.getFileInfoList();
	        	    	if(null != fileRecordList && fileRecordList.size() > 0) {
	        	    		for(FileInfo fileInfo : fileRecordList) {
	        	        		String fileId = "F" + TimeHelper.getUniqueString();
	        	        		fileInfo.setFileId(fileId);
	        	        		fileInfo.setReleteId(workordersRecordId);
	        	        		fileInfoMapper.insert(fileInfo);
	        	        	}
	        	    	}
	            	}
	        	}
	    	}
	    	//保存维修工单数据
	    	workordersInfo.setDeadlineTime(deadlineTime);
	    	workordersInfoMapper.insert(workordersInfo);
	        pubLogMapper.saveNoramlLogInfo(personName, null,LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_INSERT.getCode(),
	                String.format("添加维修工单编号:{%s} 操作成功!", new Object[] { workordersId }),
	                LogInfoDic.LOG_MODULAR_ENUM.EFOMS_WORKORDERS_MANAGEMENT.getMessage());
	        return workordersId;
    	} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
    }

    @Override
    public String updateWorkordersInfo(WorkordersInfo workordersInfo, String personName){
    	try {
	    	String workordersId = workordersInfo.getWorkordersId();
	    	workordersInfo.setUpdateDate(TimeHelper.dateToLongString());
	    	workordersInfoMapper.updateByIdSelective(workordersInfo);
	    	//先删除关联附件、后新增附件
	    	fileInfoMapper.deleteFileByReleteId(workordersId);
	    	List<FileInfo> fileInfoList = workordersInfo.getFileInfoList();
	    	if(null != fileInfoList && fileInfoList.size() > 0) {
		    	for(FileInfo fileInfo : fileInfoList) {
		    		String fileId = "F" + TimeHelper.getUniqueString();
		    		fileInfo.setFileId(fileId);
	        		fileInfo.setReleteId(workordersId);
		    		fileInfoMapper.insert(fileInfo);
		    	}
	    	}
	    	
	        pubLogMapper.saveNoramlLogInfo(personName,null, LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_UPDATE.getCode(),
	                String.format("修改维修工单编号:{%s} 操作成功!", new Object[] { workordersId }),
	                LogInfoDic.LOG_MODULAR_ENUM.EFOMS_WORKORDERS_MANAGEMENT.getMessage());
	        return workordersId;
    	} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
    }

    @Override
    public String deleteWorkordersInfo(String workordersId, String personName){
    	try {
	    	workordersInfoMapper.deleteById(workordersId);
	        pubLogMapper.saveNoramlLogInfo(personName, null,LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_DELETE.getCode(),
	                String.format("删除维修工单编号:{%s} 操作成功!", new Object[] { workordersId }),
	                LogInfoDic.LOG_MODULAR_ENUM.EFOMS_WORKORDERS_MANAGEMENT.getMessage());
	        return workordersId;
    	} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
    }

	/**
	 * 获取所有操作信息计数
	 * @return
	 */
	@Override
	public Map<String, Object> getOperationCount(String startTime,String endTime) {
		try {
			Map<String,Object> map = new HashMap<>();
	        map.put("startTime",startTime);
	        map.put("endTime",endTime);
			int fackback = workordersInfoMapper.countFackbackWKorderInfo(map);
			int overtime = workordersInfoMapper.countOverTimeWorkOder();
			int postpone = workordersInfoMapper.countPostponeWKorderInfo(map);
			int presswkorder = workordersInfoMapper.countPressWKorderInfo(map);
			int surewkorder = workordersInfoMapper.countSureWKorderInfo(map);
			int rejectwkorder = workordersInfoMapper.countRejectWKorderInfo(map);
			//待确认
			map.put(EnumMessage.ORD_MSG_SURE.getKey(),surewkorder);
			//反馈
			map.put(EnumMessage.ORD_MSG_FACKBACK.getKey(),fackback);
			//催办
			map.put(EnumMessage.ORD_MSG_PRESS.getKey(),presswkorder);
			//延期申请
			map.put(EnumMessage.ORD_MSG_POSTPONE.getKey(),postpone);
			//逾期
			map.put(EnumMessage.ORD_MSG_OVERTIME.getKey(),overtime);
			//已驳回
			map.put(EnumMessage.ORD_MSG_ISREJECT.getKey(),rejectwkorder);
			return map;
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}

	@Override
	public int countSureWKorderInfo(String startTime, String endTime) {
		try {
			Map<String,Object> map = new HashMap<>();
	        map.put("startTime",startTime);
	        map.put("endTime",endTime);
			return workordersInfoMapper.countSureWKorderInfo(map);
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}

	@Override
	public int countFackbackWKorderInfo(String startTime, String endTime) {
		try {
			Map<String,Object> map = new HashMap<>();
	        map.put("startTime",startTime);
	        map.put("endTime",endTime);
			return workordersInfoMapper.countFackbackWKorderInfo(map);
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}

	@Override
	public int countPressWKorderInfo(String startTime, String endTime) {
		try {
			Map<String,Object> map = new HashMap<>();
	        map.put("startTime",startTime);
	        map.put("endTime",endTime);
			return workordersInfoMapper.countPressWKorderInfo(map);
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}

	@Override
	public int countPostponeWKorderInfo(String startTime, String endTime) {
		try {
			Map<String,Object> map = new HashMap<>();
	        map.put("startTime",startTime);
	        map.put("endTime",endTime);
			return workordersInfoMapper.countPostponeWKorderInfo(map);
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}

	@Override
	public int countRejectWKorderInfo(String startTime, String endTime) {
		try {
			Map<String,Object> map = new HashMap<>();
	        map.put("startTime",startTime);
	        map.put("endTime",endTime);
			return workordersInfoMapper.countRejectWKorderInfo(map);
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}

	@Override
	public int countOverTimeWorkOder() {
		try {
			return workordersInfoMapper.countOverTimeWorkOder();
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}
}
